<!-- 选择小区 -->
<template>
	<view class="warpper">
		<!-- 页面内容 -->
		<view class="nl-shadow-box nl-box-padding">
			<!-- 搜索 -->
			<view class="nl-search-box">
				<view class="search">
					<u-icon class="icon" name="search" color="#D1D1D1" size="40"></u-icon>
					<u-input class="input" v-model="name" type="text" placeholder="输入搜索的小区名称" />
					<view class="button" @click="searchCommunity()">搜索</view>
				</view>
			</view>
			<!-- 小区列表 -->
			<view class="list-box">
				<view class="nl-flex-list active" v-for="(item,index) in communitys" :key="index" @click="selectCommunity(item.communityId,item.name)">
					<view class="icon"></view>
					<view class="name">{{item.name}}</view>
					<view class="right-grey">{{item.communityAddress}}</view>
				</view>
			</view>
			<u-loadmore :status="status" />
		</view>
	</view>
</template>

<script>
	import { getCommunitys } from 'api/common';
	export default {
		components: {},
		data() {
			return {
				communitys: [],	// 小区显示列表
				status: 'loadmore',	// 加载状态
				
				rowLength:'',	// 每次获取小区的列表数
				// 请求小区数据参数
				params:{
					page: 1,
					row: 15,
					cityCode:'',
					name:'',
				}
			}
		},
		onLoad(options) {
			console.log(options)
			this.params.cityCode = options.cityCode;
			this.loadCommunity()
		},
		methods: {
			/**
			 * 获取小区列表接口
			 */
			async getCommunity() {
				this.status = 'loading';
				const { data: res } = await getCommunitys(this.params);
				this.rowLength = res.list.length;
				let _data = this.communityHandle(res.list);
				if(this.rowLength < 15){
					this.status = 'nomore';
				}else{
					this.status = 'loadmore';
				}
				return _data
			},
			/**
			 * 加载小区列表
			 */
			async loadCommunity(){
				let data = await this.getCommunity();
				this.communitys.push.apply(this.communitys,data);
			},
			/**
			 * 小区数据处理
			 */
			communityHandle(arr){
				for(let i = 0; i < arr.length; i++){
					let communityAddress = arr[i].address.replace(arr[i].cityName,'');
					arr[i]['communityAddress'] = communityAddress
				}
				return arr
			},
			/**
			 * 小区搜索
			 */
			async searchCommunity(){
				this.params.page = 1;
				this.communitys = [];
				let data = await this.getCommunity();
				this.communitys = data;
			},
			/**
			 * 加载更多
			 */
			onReachBottom() {
				if(this.rowLength < 15) return;
				this.status = 'loading';
				this.params.page = ++ this.params.page;
				setTimeout(() => {
					this.loadCommunity()
					if(this.rowLength < 15) this.status = 'nomore';
					else this.status = 'loading';
				}, 1000)
			},
			/**
			 * 选择小区,并将参数传回上一个页面
			 */
			selectCommunity(communityId,communityName){
				let pages = getCurrentPages();
				let nowPage = pages[pages.length - 1];
				let prePage = pages[pages.length - 2];
				prePage.$vm.communityName = communityName;
				prePage.$vm.params.communityId = communityId;
				uni.navigateBack({
					delta:1
				})
			}
		}
	}
</script>

<style lang="scss">
	.nl-shadow-box{
		margin-bottom: 40rpx;
		padding-bottom: 20rpx;
	}
	.wrap {
		padding: 24rpx;
	}	
	
	.item {
		padding: 24rpx 0;
		color: $u-content-color;
		font-size: 28rpx;
	}
	
	.nl-search-box {
		padding: 20rpx 0;
		border-bottom: 2rpx solid $uni-bg-color-grey;

		.search {
			height: 72rpx;
			display: flex;
			align-items: center;
			position: relative;
			border-radius: 8rpx;
			overflow: hidden;
			background-color: #F3F3F3;
		}

		.icon {
			margin: 0 16rpx 0 24rpx;
		}
		
		.input {
			flex: 1;
			font-size: $uni-font-size-base;
		}

		.button {
			width: 140rpx;
			line-height: 72rpx;
			margin-left: auto;
			text-align: center;
			color: #FFFFFF;
			user-select: none;
			font-size: $uni-font-size-base;
			background-color: $nl-color-main;
		}

		.button:active {
			background-color: #fab813;
		}
	}

	.list-box {
		width: 100%;
		padding-bottom: 20rpx;
		.icon {
			width: 28rpx;
			height: 28rpx;
			background-image: url(../../../static/icon/i-xq.png);
			background-size: 100% 100%;
			background-repeat: no-repeat;
			margin-right: 16rpx;
		}
		
		.name{
			flex: 1;
			margin-right: 16rpx;
		}
		
		.right-grey{
			font-size: $uni-font-size-sm;
			text-align: right;
		}
	}
</style>
